﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using System.Data;
using DataAccess;
using Model.CustomDataType;

namespace BusinessLogic
{
    public class PatientBL: MarshalByRefObject
    {
        private PatientDA patientDA;
        /// <summary>
        /// Constructor for Patient Busines Logic tier.
        /// Note: Close this if you don't retrieve data any more use close() method to close an existing connection
        /// Always check connection before doing anything relates to SQL Connection task.
        /// </summary>
        public PatientBL()
        {
            patientDA = new PatientDA();
            
        }
        public override object InitializeLifetimeService()
        {
            return null;// live forever;
        }

        public Patient createPatient()
        {
            return new Patient();
        }
        public Patient getPatientById(int id)
        {
            if (id < 1)
            {
                return null;
            }
            else
            {
                return patientDA.getPatientById(id);
            }
        }
        
        public int addPatient(Patient patient)
        {
            if(patient==null)
            {
                return -1;
            }
            else
            {
                return patientDA.addPatient(patient);
            }
        }
        
        public bool updatePatient(Patient patient)
        {
            if(patient == null)
            {
                return false;
            }
            else
            {
                return patientDA.updatePatient(patient);
            }
        }

        public Patient[] getPatientsByDate(CMDateTime date)
        {
            int year = date.getYear();
            if (year < 1753 && year > 9999)
            {
                return null;
            }
            else return patientDA.getPatientsByDate(date);
        }
        
        public Patient[] getAllPatient()
        {
            return patientDA.getAllPatient();
        }

        public Patient[] getPatientsByRawQuery(string query)
        {
            if (query == "")
            {
                return null;
            }
            else
            {
                return patientDA.getPatientsByRawQuery(query);
            }
        }
    }
}
